package com.itheima.controller;


import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
    部门管理Controller
* */
@Slf4j  //记录日志
@RestController
@RequestMapping("/depts")
public class DeptController {
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

//    @RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
    /*
    * 查询全部的部门信息
    * List<Dept>
    * */
    @GetMapping
    public Result list(){
        log.info("完成全部部门数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /*
    * 根据Id查询部门信息
    * Dept
    * */
    @GetMapping("/{id}")
    public Result getByid(@PathVariable Integer id){
        log.info("根据id查询部门信息{}",id);
        Dept dept = deptService.getByid(id);
        return Result.success(dept);
    }

    /*
    * 添加部门数据
    * */
    @PostMapping
    @Options(keyProperty = "id",useGeneratedKeys = true)  //自动返回主键id并赋值
    public Result insertDept(@RequestBody Dept dept){
        log.info("添加部门数据");
        deptService.insertDapt(dept);
        return Result.success();
    }

    /*
    * 删除部门信息
    * */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("根据id删除部门信息{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*
    * 根据id,和name修改部门信息
    * */

    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("根据id,和name修改部门信息{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
